package com.hewep.support;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.hewep.kit.CacheKit;
import com.hewep.kit.DateUtils;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Record;

public class PrintResult {
	String[] columns;
	Map<String, String> columnRemark = CacheKit.getColumnRemark("datas");
	private int totalPage;
	private int totalRow;
	private int pageSize;
	private int pageOffset = 10;	// 偏移量
	private int firstSize ;
	private List<String> headers = new ArrayList<String>();
	private List<String> results = new ArrayList<String>();
	
	public PrintResult(Map<String, Object> head, List<Record> datas, int pageSize){
		this.firstSize = pageSize - pageOffset;
		this.pageSize = pageSize;
		this.totalRow = datas.size();
		if(firstSize == 0){
			firstSize = pageOffset;
		}
		if(totalRow < firstSize){
			firstSize = totalRow;
		}
		this.totalPage = (totalRow-firstSize)/pageSize + ((totalRow-firstSize)%pageSize==0 ? 0:1)+1;
		columns = new String[]{"serviceCode","plateNo","brand","brandTypeName",
				"outBrand","outModel"};
		firstPage(head, datas);
		if(totalPage > 1){
			preProcess(datas);
		}
	}
	
	public String toJson(){
		return "{\"totalPage\":\""+totalPage+"\","+
				" \"headers\":"+JsonKit.toJson(headers)+","+
				"\"results\":"+JsonKit.toJson(results)+"}";
	}
	private void firstPage(Map<String, Object> head,List<Record> datas){
		StringBuilder header = new StringBuilder();
//			header.append("<div id='print_head'><table><tr><td> 网点名称: "+head.get("orgName")+"</td><td><label> 省: "+head.get("province")+
//						"</label><label> 市: "+head.get("city")+"</label><label> 区: "+head.get("dist")+"</label></td></tr>"+
//						"<tr><td> 配送ID: "+head.get("id")+"</td><td> 配送地址:"+head.get("filterDealerAddr")+"</td></tr><tr><td colspan=2> 总数量:"+head.get("num")+"</td></tr></table></div>");
		String h = head.get("printHead").toString();
		String code = head.get("code").toString();
		String dealerCode = head.get("dealerCode").toString();
		String orgName = head.get("orgName").toString();
		Object orgTel = head.get("orgTel");
		orgTel = orgTel == null?"":orgTel.toString();
		String filterDealerAddr = head.get("filterDealerAddr").toString();
		String num = head.get("num").toString();
		if(num.contains(".")){
			num = num.substring(0, num.indexOf("."));
		}
		String currDate = DateUtils.getCurrDate();
		h = h.replace("{code}", code).replace("{dealerCode}", dealerCode);
		h = h.replace("{orgName}", orgName).replace("{filterDealerAddr}", filterDealerAddr);
		h = h.replace("{orgTel}", orgTel.toString()).replace("{num}", num).replace("{currDate}", currDate);
		header.append(h);
		headers.add("<div id='print_head'>"+header.toString()+"</div>");
		
		preTable(datas, 0, firstSize);
	}
	
	// 预处理
	private void preProcess(List<Record> datas){
		for (int i = 1; i < totalPage; i++) {
			int b = pageSize * i-pageOffset;
			int e = pageSize * (i+1)-pageOffset;
			if(e > totalRow){
				e = totalRow;
			}
			
			preTable(datas, b, e);
		}
	}
	
	private void preTable(List<Record> datas, int b, int e){
		StringBuilder result = new StringBuilder("<div id='print_table'><table style='border-collapse:collapse;border:1px solid black;'>");
		result.append("<thead>");
		int percent = 100 / (columns.length+2);
		for (int j = 0; j < columns.length; j++) {
			result.append("<th nowrap align='left' width='"+percent+"%'>"+columnRemark.get(columns[j])+"</th>");
		}
		result.append("<th nowrap align='left' width='"+percent+"%'>数量</th>");
		result.append("<th nowrap align='left' width='"+percent+"%'>备注</th>");
		
		result.append("</thead>");
		result.append("<tbody>");
		for (int j = b; j < e; j++) {
			Record data = datas.get(j);
			result.append("<tr>");
			for (int k = 0; k < columns.length; k++) {
				result.append("<td nowrap>"+data.get(columns[k])+"</td>");
			}
			result.append("<td nowrap>1</td>");
			result.append("<td nowrap></td>");
			result.append("</tr>");
		}
		result.append("</tbody>");
		result.append("</table></div>");
		
		results.add(result.toString());
	}
}
